<template>
  <div class="bank-card-management">
    <el-card>
      <el-form ref="ruleForm" :model="ruleForm" :inline="true" label-width="100px" class="ruleForm">
        <el-form-item label="审核状态：" prop="examineStatus">
          <el-select v-model="ruleForm.examineStatus">
            <el-option label="全部" value="" />
            <el-option label="审核中" value="IN_REVIEW" />
            <el-option label="审核通过" value="VERIFIED" />
            <el-option label="审核不通过" value="REJECTED" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="platform">平台银行卡</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="isLoading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          prop="user_id"
          label="商家ID"
          width="120"
          align="center"
        />
        <el-table-column
          prop="amount"
          label="转账金额"
          width="150"
          align="center"
        />
        <el-table-column
          prop="account_number"
          label="转账卡号"
          width="250"
          align="center"
        />
        <el-table-column
          prop="account_username"
          label="转账人"
          width="120"
          align="center"
        />
        <el-table-column
          prop="transfer_time"
          label="转账时间"
          width="200"
          align="center"
        />
        <el-table-column
          label="转账图片"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <input-image :src="scope.row.transfer_picture" />
          </template>
        </el-table-column>
        <el-table-column
          prop="examine_explain"
          label="审核说明"
          align="center"
        />
        <el-table-column
            label="状态"
          align="center"
          width="120"
          >
            <template slot-scope="scope">
              {{ scope.row.examine_status | getExamineStatus }}
            </template>
          </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.examine_status === 'IN_REVIEW'" type="text" size="small" @click="handleClick(scope.row, scope.$index, 'VERIFIED')">通过</el-button>
            <el-button v-if="scope.row.examine_status === 'IN_REVIEW'" type="text" size="small" @click="handleClick(scope.row, scope.$index, 'REJECTED')">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog
      title="平台银行卡管理"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="bankForm" :rules="bankRules" ref="bankRuleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="银行卡号" prop="accountNumber">
          <el-input v-model="bankForm.accountNumber"></el-input>
        </el-form-item>
        <el-form-item label="开户人" prop="accountUsername">
          <el-input v-model="bankForm.accountUsername"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="bankForm.bankName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="bankSubmitForm('bankRuleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { transferSearch, transferExamine, getBankAccount, updateBankAccount } from '@/api/user'
import inputImage from '@/components/_inputImage'
export default {
  components: {
    inputImage
  },
  filters: {
    getExamineStatus(status) {
      let StringType = ''
      switch(status) {
        case 'IN_REVIEW' :
          StringType = '审核中'
          break
        case 'VERIFIED' :
          StringType = '审核通过'
          break
        case 'REJECTED' :
          StringType = '审核不通过'
          break
      }
      return StringType
    }
  },
  data() {
    return {
      isLoading: false,
      bankLoading: false,
      tableData: [],
      ruleForm: {
        examineStatus: '', // IN_REVIEW-审核中，VERIFIED-审核通过，REJECTED-审核不通过
        pageNumber: 1
      },
      total: 0,
      dialogVisible: false,
      bankForm: {},
      bankRules: {}
    }
  },
  mounted() {
    this.transferSearch()
  },
  methods: {
    transferSearch() {
      this.isLoading = true
      transferSearch(this.ruleForm).then(res => {
        this.tableData = res.list
        this.total = res.totalRow
        this.isLoading = false
      }).catch(() => {
        this.isLoading = false
      })
    },
    handleClick(row, index, type) {
      this.$prompt(`是否${type === 'VERIFIED' ? '通过' : '不通过'}该转账的审核？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        transferExamine({
          id: row.id,
          examineStatus: type,
          examineExplain: value
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.tableData[index].examine_status = type
          this.tableData[index].examine_explain = value
        })
      })
    },
    platform() {
      this.dialogVisible = true
      this.bankLoading = true
      getBankAccount().then(res => {
        console.log(res)
        this.bankForm = res
        this.bankLoading = false
      }).catch(() => {
        this.bankLoading = false
      })
    },
    onSubmit() {
      this.ruleForm.pageNumber = 1
      this.transferSearch()
    },
    handleCurrentChange(page) {
      this.ruleForm.pageNumber = page
      this.transferSearch()
    },
    bankSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateBankAccount(this.bankForm).then(res => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
          })
          this.dialogVisible = false
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>
<style lang="scss">
  .bank-card-management{
    .el-card{
      .el-form{
        .el-input{
          width: 200px;
        }
        .el-select{
          width: 200px;
        }
      }
    }
  }
</style>
